package com.yxhl.zoume.core.expresscar.helper;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.utils.PopupWindowUtils;

/**
 * Author：zhongshan   
 * Date：2016/12/12 14:54      
 * Description：
 */
public class DriverPhonePopupWindow implements View.OnClickListener {

    private Builder mBuilder;
    private PopupWindow mPhonePopupWindow;

    public DriverPhonePopupWindow(Builder builder) {
        mBuilder = builder;
        initPopUpWindow(builder);
    }

    private void initPopUpWindow(Builder builder) {
        View contentView = LayoutInflater.from(builder.mContext).inflate(R.layout.popup_special_car_schedule_phone, null);
        TextView mPopupTitleTv = (TextView) contentView.findViewById(R.id.tv_popup_special_car_schedule_title);
        TextView mPopupContentTv = (TextView) contentView.findViewById(R.id.tv_popup_special_car_schedule_content);
        TextView mPopupCancelTv = (TextView) contentView.findViewById(R.id.tv_popup_special_car_schedule_cancel);
        TextView mPopupDialTv = (TextView) contentView.findViewById(R.id.tv_popup_special_car_schedule_dial);

        if (!TextUtils.isEmpty(builder.getTitle())) {
            mPopupTitleTv.setText(builder.getTitle());
        }

        if (!TextUtils.isEmpty(builder.getContent())) {
            mPopupContentTv.setText(builder.getContent());
        }

        if (!TextUtils.isEmpty(builder.getCancel())) {
            mPopupCancelTv.setText(builder.getCancel());
            mPopupCancelTv.setOnClickListener(this);
        }

        if (!TextUtils.isEmpty(builder.getEnsure())) {
            mPopupDialTv.setText(builder.getEnsure());
            mPopupDialTv.setOnClickListener(this);
        }
        mPhonePopupWindow = PopupWindowUtils.getSchedulePhonePopupWindow(contentView);
        mPhonePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowBackgroundAlpha(1f);
            }
        });
    }

    public void showAtBottomLocation(View parent) {
        setWindowBackgroundAlpha(0.5f);
        mPhonePopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 设置窗口透明度
     *
     * @param bgAlpha
     */
    private void setWindowBackgroundAlpha(float bgAlpha) {
        Activity mActivity = (Activity) mBuilder.mContext;
        WindowManager.LayoutParams attributes = mActivity.getWindow().getAttributes();
        attributes.alpha = bgAlpha;
        mActivity.getWindow().setAttributes(attributes);
    }


    public static class Builder {
        private String title;
        private String content;
        private String cancel;
        private String ensure;
        private Context mContext;
        private SingleButtonCallback cancelButtonCallback;
        private SingleButtonCallback ensureButtonCallback;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setCancel(String cancel) {
            this.cancel = cancel;
            return this;
        }

        public Builder setEnsure(String ensure) {
            this.ensure = ensure;
            return this;
        }

        public String getTitle() {
            return title;
        }

        public String getContent() {
            return content;
        }

        public String getCancel() {
            return cancel;
        }

        public String getEnsure() {
            return ensure;
        }


        public Builder onNegative(SingleButtonCallback cancelButtonCallback) {
            this.cancelButtonCallback = cancelButtonCallback;
            return this;
        }

        public Builder onPositive(SingleButtonCallback ensureButtonCallback) {
            this.ensureButtonCallback = ensureButtonCallback;
            return this;
        }

        public DriverPhonePopupWindow build() {
            return new DriverPhonePopupWindow(this);
        }
    }

    public interface SingleButtonCallback {

        void onClick(@NonNull DriverPhonePopupWindow popupWindow);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_popup_special_car_schedule_cancel:
            default:
                if (mBuilder.cancelButtonCallback != null) {
                    mBuilder.cancelButtonCallback.onClick(this);
                }
                break;
            case R.id.tv_popup_special_car_schedule_dial:
                if (mBuilder.ensureButtonCallback != null) {
                    mBuilder.ensureButtonCallback.onClick(this);
                }
                break;
        }

    }

    public void dismiss() {
        if (mPhonePopupWindow != null) {
            if (mPhonePopupWindow.isShowing()) {
                mPhonePopupWindow.dismiss();
            }
        }
    }
}
